﻿using NHibernate.Driver;
using System.Data;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate.Util;
using NHibernate;
using NHibernate.Dialect;
using System.Data.Common;

namespace Inovout.Gis.Data
{
    public abstract class GisDriver : DriverBase
    {
        private DriverBase rmdbDrive;
        public override IDbCommand CreateCommand()
        {
            IDbCommand command = new GisCommand(rmdbDrive);
            return command;
        }

        public override void Configure(IDictionary<string, string> settings)
        {
            //1、获取断言
            //2、获取类型
            //3、实例化
            //4、配置Settings
            string dialectrClass;
            if (!settings.TryGetValue(Environment.Dialect, out dialectrClass))
            {
                throw new HibernateException("The " + Environment.Dialect +
                                             " must be specified in the NHibernate configuration section.");
            }
            else
            {
                Dialect dialect = Dialect.GetDialect(settings);
                string driverClass = dialect.DefaultProperties[Environment.ConnectionDriver];
                this.rmdbDrive = (DriverBase)Environment.BytecodeProvider.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(driverClass));
                this.rmdbDrive.Configure(settings);
            }
        }

        public override IDbConnection CreateConnection()
        {
            IDbConnection connection =new GisConnection(rmdbDrive);
            return connection;
        }

        public override string NamedPrefix
        {
            get { return rmdbDrive.NamedPrefix; }
        }

        public override bool UseNamedPrefixInParameter
        {
            get { return rmdbDrive.UseNamedPrefixInParameter; }
        }

        public override bool UseNamedPrefixInSql
        {
            get { return rmdbDrive.UseNamedPrefixInSql; }
        }
    }
}
